package com.ruoyi.web.controller.airplane;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.TbAppCatalog;
import com.ruoyi.system.domain.dto.ItemDTO;
import com.ruoyi.system.domain.dto.TbAppVersionCreateDTO;
import com.ruoyi.system.domain.vo.TbAppCatlogAndVersionVO;
import com.ruoyi.system.domain.vo.TbAppVersionVO;
import com.ruoyi.system.domain.vo.TbDeviceInfoVO;
import com.ruoyi.system.domain.vo.TbTaskDetailVO;
import com.ruoyi.system.service.ITbDeviceAppsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbAppVersion;
import com.ruoyi.system.service.ITbAppVersionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 应用版本信息Controller
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Api(tags = "应用版本信息")
@RestController
@RequestMapping("/system/version")
public class TbAppVersionController extends BaseController {
    @Autowired
    private ITbAppVersionService tbAppVersionService;

    @Autowired
    private ITbDeviceAppsService tbDeviceAppsService;

    /**
     * 查询应用版本信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:version:list')")
    @ApiOperation("查询应用版本信息列表")
    @GetMapping("/list")
    public TableDataInfo list(TbAppVersion tbAppVersion) {
        startPage();
        List<TbAppVersion> list = tbAppVersionService.selectTbAppVersionList(tbAppVersion);
        return getDataTable(list);
    }


    @ApiOperation("查询应用版本信息列表/不分页")
    @GetMapping("/lis")
    public R<List<TbAppVersionVO>> listNoPage(TbAppVersion tbAppVersion) {
        List<TbAppVersionVO> list = tbAppVersionService.selectTbAppVersionLis(tbAppVersion);
        return R.ok(list);
    }


    /**
     * 导出应用版本信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:version:export')")
    @ApiOperation("导出应用版本信息列表")
    @Log(title = "应用版本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAppVersion tbAppVersion) {
        List<TbAppVersion> list = tbAppVersionService.selectTbAppVersionList(tbAppVersion);
        ExcelUtil<TbAppVersion> util = new ExcelUtil<TbAppVersion>(TbAppVersion.class);
        util.exportExcel(response, list, "应用版本信息数据");
    }

    /**
     * 获取应用版本信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:version:query')")
    @ApiOperation("获取应用版本信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tbAppVersionService.selectTbAppVersionById(id));
    }

    /**
     * 新增应用版本信息
     */
    //@PreAuthorize("@ss.hasPermi('system:version:add')")
    @ApiOperation("新增应用版本信息")
    @Log(title = "应用版本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody TbAppVersionCreateDTO dto) {

        return  tbAppVersionService.insertTbAppVersion(dto);
    }

    /**
     * 修改应用版本信息
     */
    //  @PreAuthorize("@ss.hasPermi('system:version:edit')")
    @ApiOperation("修改应用版本信息")
    @Log(title = "应用版本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAppVersion tbAppVersion) {
        return toAjax(tbAppVersionService.updateTbAppVersion(tbAppVersion));
    }

    /**
     * 删除应用版本信息
     */
    //@PreAuthorize("@ss.hasPermi('system:version:remove')")
    @ApiOperation("批量删除-删除应用版本信息")
    @Log(title = "应用版本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbAppVersionService.deleteTbAppVersionByIds(ids));
    }


    /**
     * 删除设备安装应用信息
     */
    @ApiOperation("对应版本的应用关联的设备信息列表")
    //  @PreAuthorize("@ss.hasPermi('system:apps:remove')")
    @PostMapping("/versionCode/device")
    public TableDataInfo listBypackageVersioncode(@RequestBody ItemDTO dto) {
        startPage();

        List<TbDeviceInfoVO> list = tbDeviceAppsService.listBypackageVersioncode(dto);

        return getDataTable(list);

    }


    @ApiOperation("对应版本的应用关联的任务列表")
    //  @PreAuthorize("@ss.hasPermi('system:apps:remove')")
    @PostMapping("/versionCode/taskDetail")
    public TableDataInfo listTaskBypackageVersioncode(@RequestBody ItemDTO dto) {
        startPage();

        List<TbTaskDetailVO> list = tbDeviceAppsService.listTaskBypackageVersioncode(dto);

        return getDataTable(list);

    }


}
